package com.ghsc.ghxc.jsb.ui.home

import android.graphics.Typeface
import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.cn.appcore.adapter.PagerAdapter
import com.cn.appcore.base.BaseVMFragment
import com.cn.appcore.ext.showToast
import com.flyco.tablayout.listener.OnTabSelectListener
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.FragmentPageBinding

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.home
 * 类名：HomeFragment
 * 创建时间：2024/4/23 15:00
 * 创建人 @author： xiaolin
 * 描述 ：Todo...
 */
class HomeFragment : BaseVMFragment<HomeViewModel, FragmentPageBinding>(R.layout.fragment_page),
    OnTabSelectListener {
    override fun attachVMClass(): Class<HomeViewModel>? {
        return HomeViewModel::class.java
    }

    override fun lazyLoad() {
    }


    var mList: MutableList<String> = ArrayList()
    var mAdapter: PagerAdapter? = null
    private val mFragments: MutableList<Fragment> = ArrayList()


    override fun initView(savedInstanceState: Bundle?) {
        mViewModel.onTabTypeLunBoMethod("")
    }
    override fun onTabSelect(position: Int) {
        tabIndex(position)
    }

    override fun onTabReselect(position: Int) {
    }
    private fun tabIndex(index: Int) {
        for (i in mList.indices) {
            if (i == index) {
                val tv = binding.slitab.getTitleView(i)
                tv.textSize = 16f
                tv.typeface = Typeface.defaultFromStyle(Typeface.BOLD) //加粗
            } else {
                val tv2 = binding.slitab.getTitleView(i)
                tv2.textSize = 14f
                tv2.typeface = Typeface.defaultFromStyle(Typeface.NORMAL)
            }
        }
    }

    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@HomeFragment) {
                showToast(it)
            }
            againLoginMsg.observe(this@HomeFragment) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onTabTypeLunBoData.observe(this@HomeFragment) {
                mList.clear()
                mList.add("推荐")
                mFragments.clear()
                ListRecommendFragment.getInstance(0)?.let { mFragments.add(it) }
                if (it.data.data.topcat.isNotEmpty()){
                    for (i in it.data.data.topcat.indices){
                        mList.add(it.data.data.topcat[i].catename)
                        ListTypesFragment.getInstance(it.data.data.topcat[i].cid)?.let { mFragments.add(it) }
                    }
                }
                mAdapter = PagerAdapter(
                    childFragmentManager,
                    mList,
                    mFragments
                )
                binding.viewPager.adapter = mAdapter
                binding.viewPager.offscreenPageLimit = mList.size - 1
                binding.slitab.setViewPager(binding.viewPager)
                binding.slitab.setOnTabSelectListener(this@HomeFragment)
                binding.slitab.currentTab = 0
                binding.viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
                    override fun onPageScrolled(
                        position: Int,
                        positionOffset: Float,
                        positionOffsetPixels: Int
                    ) {
                    }

                    override fun onPageSelected(position: Int) {
                        tabIndex(position)
                    }

                    override fun onPageScrollStateChanged(state: Int) {

                    }
                })
                tabIndex(0)

            }
        }
    }
}